/**

 * @Description:TODO

 * @author:liyang

 * @time:2017年8月7日 下午1:53:08

 */
package serverlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.cxf.common.logging.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.thor.palmmall.hdmall.media.HDMMediaService;

/**
 * @author:liyang
 * @time:2017年8月7日 下午1:53:08
 * @since:1.0
 */
@SuppressWarnings("serial")
public class DownloadImageServlet extends HttpServlet {

  private static final Logger LOG = LogUtils.getLogger(DownloadImageServlet.class);

  HDMMediaService mediaService;

  @Override
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String moduleName = request.getParameter("moduleName");
    String num = request.getParameter("num");
    String fileName = request.getParameter("fileName");

    try {
      OutputStream os = response.getOutputStream();
      byte[] byteArr = mediaService.download(moduleName, num, fileName);
      os.write(byteArr);
      os.flush();
    } catch (Exception e) {
      LOG.log(Level.ALL, e.getMessage());
    }
  }

  @Override
  public void init(ServletConfig servletConfig) throws ServletException {
    WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletConfig.getServletContext());
    mediaService = (HDMMediaService) wac.getBean(HDMMediaService.class);
  }
}